import dayjs from 'dayjs'
import duration from 'dayjs/plugin/duration';
 
// 初始化时间
dayjs.extend(duration);


function toDayTime(targetTime){
  if(targetTime &&targetTime > 2000 * 100){
   return  dayjs(targetTime)
  }
  return  dayjs()
}

/**
 *  当前时间与目标时间相差多少分析
 * @param { 目标时间戳} targetTime 
 * @returns 
 */
export function diffMinutes(targetTime) {
  const now = dayjs();
  const target = toDayTime(targetTime);
  const diff = dayjs.duration(now .diff(target));
  const days = Math.abs(diff.days())
  const hours = Math.abs(diff.hours())
  const minutes = Math.abs(diff.minutes())

  var  timeStr = '';
  if(days>0){
    timeStr += `${days}天`
  }
  if(hours>0){
    timeStr += `${hours}时`
  }
  if(minutes>=0){
    timeStr += `${minutes}分`
  }
  return timeStr
}


/**
 *  当前时间与目标时间相差多少分析
 * @param { 目标时间戳} targetTime 
 * @returns 
 */
export function formatTime(targetTime) {
  return    toDayTime(targetTime).format('YYYY-MM-DD HH:mm:ss') 
}

